/**********************************************************
 * StrutsFilterDispacher.java
 * Created by ankur
 * Last modified Date: Aug 26, 2008 11:26:13 AM by ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2008 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

/**
 * This class loads all default applications parameters.
 * SYSTEM_ACCESS = Defines access to Site for USERS.
 * SYSTEM_ACCESS = true. Defines that Site is available for USER'S
 * access is used to set the default value for SYSTEM_ACCESS.
 */

package com.printeverywhere.util;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;

import org.apache.log4j.Logger;
import org.apache.struts2.dispatcher.FilterDispatcher;

import com.printeverywhere.delegator.AdditionalCategoriesServiceDelegator;
import com.printeverywhere.delegator.AdditionalOptionsFeesServiceDelegator;
import com.printeverywhere.delegator.AuctionListingFeesServiceDelegator;
import com.printeverywhere.delegator.CategoryManagerServiceDelegator;
import com.printeverywhere.delegator.CategoryOptionsServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.ConfigurationServiceDelegator;
import com.printeverywhere.delegator.FinalFeesServiceDelegator;
import com.printeverywhere.delegator.GeneralOptionsServiceDelegator;
import com.printeverywhere.delegator.ListingOptionsServiceDelegator;
import com.printeverywhere.delegator.LocationCategoriesServiceDelegator;
import com.printeverywhere.delegator.MailingSystemServiceDelegator;
import com.printeverywhere.delegator.MiscellaneousFeesServiceDelegator;
import com.printeverywhere.delegator.ReserveAuctionListingFeesServiceDelegator;
import com.printeverywhere.delegator.TranslationSystemServiceDelegator;
import com.printeverywhere.delegator.UserRegistrationOptionsDelegator;
import com.printeverywhere.dto.AffiliateSettings;
import com.printeverywhere.dto.MerchantAccount;
import com.printeverywhere.dto.PrivateMailOption;

/**
 * The Class StrutsFilterDispacher.
 */
public class StrutsFilterDispacher extends FilterDispatcher {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(StrutsFilterDispacher.class);

	// It makes site available to user default
	// To lock site in default case make access = false
	/* (non-Javadoc)
	 * @see org.apache.struts2.dispatcher.FilterDispatcher#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		if (logger.isDebugEnabled()) {
			logger.debug("init(FilterConfig) - start"); //$NON-NLS-1$
		}

		filterConfig.getServletContext().setAttribute(AdminConstants.SYSTEM_ACCESS, AdminConstants.DEFAULT_ACCESS);
		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute("active", Boolean.TRUE);
		searchCriteria.setAsc(true);
		searchCriteria.setOrderBy("languageName");
		filterConfig.getServletContext().setAttribute(AdminConstants.LANGUAGES, TranslationSystemServiceDelegator.getList(searchCriteria));
		filterConfig.getServletContext().setAttribute(AdminConstants.CONFIGURATION, ConfigurationServiceDelegator.getConfiguration());
		filterConfig.getServletContext().setAttribute(AdminConstants.CATEGORY_OPTIONS, CategoryOptionsServiceDelegator.getCategoryOptions());
		filterConfig.getServletContext().setAttribute(AdminConstants.LISTING_OPTIONS, ListingOptionsServiceDelegator.getListingOptions());
		filterConfig.getServletContext().setAttribute(AdminConstants.GENERAL_OPTIONS, GeneralOptionsServiceDelegator.getGeneralOptions());
		filterConfig.getServletContext().setAttribute(AdminConstants.MAILING_OPTIONS, MailingSystemServiceDelegator.getMailingSystem());
		filterConfig.getServletContext().setAttribute(AdminConstants.MISCELLANEOUS_FEES, MiscellaneousFeesServiceDelegator.getMiscellaneousFees());
		filterConfig.getServletContext().setAttribute(AdminConstants.REGULAR_AUCTION_OPTIONS, AuctionListingFeesServiceDelegator.getAuctionListingFees());
		filterConfig.getServletContext().setAttribute(AdminConstants.FINAL_FEES, FinalFeesServiceDelegator.getFinalFees());
		filterConfig.getServletContext().setAttribute(AdminConstants.RESERVE_FEES, ReserveAuctionListingFeesServiceDelegator.getReserveAuctionListingFees());
		filterConfig.getServletContext().setAttribute(AdminConstants.OPTIONS_FEES, AdditionalOptionsFeesServiceDelegator.getAdditionalOptionsFees());
		filterConfig.getServletContext().setAttribute(AdminConstants.CATEGORY_FEES, AdditionalCategoriesServiceDelegator.getAdditionalCategories());
		filterConfig.getServletContext().setAttribute(AdminConstants.PAYMENT_GATEWAYS, CommonFunctionDelegator.getAllfromtable(MerchantAccount.class.getName()));
		filterConfig.getServletContext().setAttribute(AdminConstants.REGISTRATION_OPTIONS, UserRegistrationOptionsDelegator.getList());
		filterConfig.getServletContext().setAttribute(AdminConstants.AFFILIATE_PROGRAM, CommonFunctionDelegator.getObjectById("id", 1, AffiliateSettings.class));
		filterConfig.getServletContext().setAttribute(AdminConstants.PRIVATE_MAIL_OPTIONS, CommonFunctionDelegator.getAllfromtable(PrivateMailOption.class.getName()));

		SearchListCriteria searchListCriteria = new SearchListCriteria();
		searchListCriteria.setOrderBy(AdminConstants.SORTORDER);
		searchListCriteria.setAsc(AdminConstants.TRUE);
		filterConfig.getServletContext().setAttribute(AdminConstants.LOCATIONCATEGORIES, LocationCategoriesServiceDelegator.getSortedList(searchListCriteria));

		filterConfig.getServletContext().setAttribute(AdminConstants.GENERALCATEGORIES, CategoryManagerServiceDelegator.getSortedList(searchListCriteria));

		searchCriteria.removeAllAttribute();
		searchCriteria.setAttribute("active", Boolean.TRUE);
		searchCriteria.setOrderBy(AdminConstants.SORTORDER);
		searchCriteria.setAsc(AdminConstants.TRUE);
		filterConfig.getServletContext().setAttribute(AdminConstants.FIND_GENERALCATEGORIES, CategoryManagerServiceDelegator.getList(searchCriteria));

		searchCriteria.removeAllAttribute();
		searchCriteria.setAttribute("level", 1);
		searchCriteria.setAttribute("active", Boolean.TRUE);
		searchCriteria.setOrderBy(AdminConstants.SORTORDER);
		searchCriteria.setAsc(AdminConstants.TRUE);
		filterConfig.getServletContext().setAttribute(AdminConstants.GENERALCATEGORIES_LEFT, CategoryManagerServiceDelegator.getList(searchCriteria));
		super.init(filterConfig);

		if (logger.isDebugEnabled()) {
			logger.debug("init(FilterConfig) - end"); //$NON-NLS-1$
		}
	}
}